#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.articleController import ArticleController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class ArticleResource(Resource):

    # get
    @classmethod
    def get(cls, article_id=None):
        if article_id:
            kwargs = {
                'article_id': article_id
            }

            res = ArticleController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('article_id', location='args', required=False, help='article_id参数类型不正确或缺失')
        parser.add_argument('title', location='args', required=False, help='title参数类型不正确或缺失')
        parser.add_argument('psy_id', location='args', required=False, help='psy_id参数类型不正确或缺失')
        parser.add_argument('publish_time', location='args', required=False, help='publish_time参数类型不正确或缺失')
        parser.add_argument('content', location='args', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = ArticleController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    
    # delete
    @classmethod
    def delete(cls, article_id=None):
        if article_id:
            kwargs = {
                'article_id': article_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = ArticleController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # put
    @classmethod
    def put(cls, article_id):
        if not article_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('title', location='json', required=False, help='title参数类型不正确或缺失')
        parser.add_argument('psy_id', location='json', required=False, help='psy_id参数类型不正确或缺失')
        parser.add_argument('publish_time', location='json', required=False, help='publish_time参数类型不正确或缺失')
        parser.add_argument('content', location='json', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('create_time', location='json', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='json', required=False, help='update_time参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['article_id'] = article_id

        res = ArticleController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # add
    @classmethod
    def post(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('title', location='json', required=True, help='title参数类型不正确或缺失')
        parser.add_argument('psy_id', location='json', required=True, help='psy_id参数类型不正确或缺失')
        parser.add_argument('content', location='json', required=True, help='content参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = ArticleController.add(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])
